<?php
class AuthController extends Zend_Controller_Action
{
	protected $_namespace = 'RegistrationController';
    protected $_session;
	public function homeAction()
    {
	$storage = new Zend_Auth_Storage_Session();
    $data = $storage->read();
        if(!$data){
            $this->_redirect('auth/login');
        }
       	$this->view->username = $data->username;                


	}
	public function getSessionNamespace()
    {
        if (null === $this->_session) {
            $this->_session =
                new Zend_Session_Namespace($this->_namespace);
        }
 
        return $this->_session;
    }
	public function forgotAction()
	{
		$this->_helper->layout->setLayout('index_layout');
		$users = new AML_Model_Clients();
        $form = new AML_Form_ForgotForm();
        $this->view->form = $form;
        if($this->getRequest()->isPost()){
            if($form->isValid($_POST)){
                $data = $form->getValues();
				//print_r($data);
				$data = $users->fetchRow("reg_email='".$data['reg_email']."'");	
				if($data->reg_id != "")
				{
					// email module
					$setting_obg = new AML_Model_SettingsTable();
					$settings_1 = $setting_obg->getSettings();
					//print_r($settings_1);
					$email_obj = new AML_Model_TemplatesTable();
					$row_email = $email_obj->getEmail(6); // change this value accordingly 
					$subject = $row_email['temp_subject'];
					$message = $row_email['temp_text'];
					$orignal = array("#NAME#","#USERNAME#","#PASSWORD#","#SYSTEM-NAME#");
					$replace = array($data['reg_fname'],$data['reg_email'],$data['reg_pass'],$settings_['config_tag']);
					$new_message = str_replace($orignal, $replace, $message);
					#send email
					$mail = new Zend_Mail();
					$mail->setFrom ($settings_1['config_email'], $settings_1['config_tag']);
					$mail->addTo($data['reg_email'], $data['reg_fname']);
					$mail->addBcc($settings_1['config_email']);
					$mail->setSubject($subject);
					$mail->setBodyHtml($new_message);
					$mail->send();
					//end of email module
					$this->view->successMessage = "Please check your email account";
				}
				else
				{
					$this->view->errorMessage = "<strong>Error(s):</strong><br>Invalid Email address, please retype";
				}
			}
		}

	}
	public function loginAction()
    {
        $storage = new Zend_Auth_Storage_Session();
        $data = $storage->read();
       	if($this->_getParam("success"))
			$this->view->successMessage = 
		$this->_getParam("success");
		print_r($data);
		//exit;
	
		if($data){
            $this->_redirect('/myaccount/');
        }
		
		$this->_helper->layout->setLayout('index_layout');
		$users = new AML_Model_Clients();
        $form = new AML_Form_UserLoginForm();
		$this->view->form = $form;
         $formReg = new AML_Form_RegistrationForm();
        $this->view->formReg=$formReg;
        if($this->getRequest()->isPost()){
        		if($form->isValid($_POST)){
                $data = $form->getValues();
               // print_r($data);die();
				$auth = Zend_Auth::getInstance();
				//print_r($auth);
                $authAdapter = new Zend_Auth_Adapter_DbTable($users->getAdapter(),'aml_client_reg');
                //print_r($authAdapter);
			    $authAdapter->setIdentityColumn('reg_email')
                            ->setCredentialColumn('reg_pass');
				              
				
$authAdapter->setIdentity($data['reg_username'])
                           ->setCredential($data['reg_pass']);
				
                $result = $auth->authenticate($authAdapter);
                //die();
                if($result->isValid()){
				    $storage = new 
Zend_Auth_Storage_Session();
                    $storage->write($authAdapter->getResultRowObject());
                    $this->_redirect('/myaccount/');
                } else {
                    $this->view->errorMessage = "<spam 
class='error'>Invalid username or password. Please try again.</spam>";
                }      
				   
            	
            }
        }
    } 
	public function reloginAction()
    {
        $storage = new Zend_Auth_Storage_Session();
        $data = $storage->read();
       	if($this->_getParam("success"))
			$this->view->successMessage = 
		$this->_getParam("success");
		print_r($data);
		//exit;
	
		if($data){
            $this->_redirect('/myaccount/');
        }
		
		$this->_helper->layout->setLayout('index_layout');
		$users = new AML_Model_Clients();
        $form = new AML_Form_UserLoginForm();
		$this->view->form = $form;
         $formReg = new AML_Form_RegistrationForm();
        $this->view->formReg=$formReg;
        if($this->getRequest()->isPost()){
        		if($form->isValid($_POST)){
                $data = $form->getValues();
               // print_r($data);die();
				$auth = 
Zend_Auth::getInstance();print_r($auth);
                $authAdapter = new 
Zend_Auth_Adapter_DbTable($users->getAdapter(),'aml_client_reg');
                //print_r($authAdapter);
			    $authAdapter->setIdentityColumn('reg_email')
                            ->setCredentialColumn('reg_pass');
				              
				
$authAdapter->setIdentity($data['reg_username'])
                           ->setCredential($data['reg_pass']);
				
                $result = $auth->authenticate($authAdapter);
                //die();
                if($result->isValid()){
				    $storage = new 
Zend_Auth_Storage_Session();
                    $storage->write($authAdapter->getResultRowObject());
                    $this->_redirect('/myaccount/');
                } else {
                    $this->view->errorMessage = "<spam 
class='error'>Invalid username or password. Please try again.</spam>";
                }      
				   
            	
            }
        }
    } 
	
	public function signupAction()
    {
		 $this->_helper->layout->setLayout('index_layout');
        $users = new AML_Model_Clients();
		$form = new AML_Form_UserLoginForm();
		$this->view->form = $form;
        $formReg = new AML_Form_RegistrationForm();
        $this->view->formReg=$formReg;
        if($this->getRequest()->isPost()){
            if($formReg->isValid($_POST)){
                $data = $formReg->getValues();
                if($data['reg_pass'] != $data['admin_confirmPassword']){
                    $this->view->error = "Password and confirm password don't match.";
                    return;
                }
               
                if($users->checkUnique($data['reg_email'])){
                    $this->view->error = "Email address already taken. Please choose another one.";
                    return;
                }
                unset($data['admin_confirmPassword']);
                 echo "<pre>";
                print_r($data);
                echo "</pre>";//die();
			  $stepone =  array('reg_pass' 	=> $data['reg_pass'],
			   		 'reg_email'	=>$data['reg_email']);
			   $this->getSessionNamespace()->stepone = $stepone;                //$users->insert($data);
                $this->_redirect('auth/stepone');
            }
        }
        else {
       
        }
    }
    public function steponeAction()
    {
         $this->_helper->layout->setLayout('index_layout');
		 $this->view->errorMessage = "<spam class='error'>Invalid username or password. Please try again.</spam>";
    } 
	public function steptwoAction()
    {
		$form = new AML_Form_RegistrationFormStepTwo();
		/*echo "<pre>";
		print_r($_POST);
		echo "</pre>";//die();*/
		if ($this->_request->isPost()) {
			if ($form->isValid($this->_request->getParams())) 
			{
			//$sessionNamespace = new Zend_Session_Namespace();
			$steptwodata = array('reg_fname' 	=> $this->_request->getPost('fname'),
								 'reg_mname' 	=> $this->_request->getPost('mname'),
								 'reg_lname' 	=> $this->_request->getPost('lname'),
								 //'reg_email' 	=> $this->_request->getPost('email'),
								 'reg_add1' 	=> $this->_request->getPost('address1'),
								 'reg_add2' 	=> $this->_request->getPost('address1'),
								 'reg_city' 	=> $this->_request->getPost('city'),
								 'reg_state' 	=> $this->_request->getPost('state'),
								 'reg_pcode' 	=> $this->_request->getPost('zip'),
								 'reg_country' 	=> $this->_request->getPost('country'),
								 'reg_ph' 	=> $this->_request->getPost('phone'));
				$this->getSessionNamespace()->steptwo = $steptwodata;
				$this->_redirect('auth/stepthree'); 
				
			} else {
				$this->view->errors = $form->getMessages();
			}
		}
        $this->view->form=$form;
		$this->_helper->layout->setLayout('index_layout');
    } 
	
	public function stepthreeAction()
    {
        
		$form = new AML_Form_RegistrationFormStepThree();
		if ($this->_request->isPost()) {
			if ($form->isValid($this->_request->getParams())) {
				$stepthreedata = array(
							'reg_biilingaddress_same_as_contact' 	=> $this->_request->getPost('same_as_contact'),
							'reg_card_holder_name' 	=> $this->_request->getPost('billname'),
							'reg_bill_address1' 	=> $this->_request->getPost('billaddress1'),
							'reg_bill_address2' 	=> $this->_request->getPost('billaddress2'),
							'reg_bill_city' 		=> $this->_request->getPost('billcity'),
							'reg_bill_state' 		=> $this->_request->getPost('billstate'),
							'reg_bill_country' 		=> $this->_request->getPost('billcountry'),
							'reg_bill_phone' 	=> $this->_request->getPost('billphone'),
							'reg_bill_email' 		=> $this->_request->getPost('billemail'),
							'reg_bill_cardtype' 	=> $this->_request->getPost('cardtype'),
							'reg_bill_cardno' 	=> $this->_request->getPost('cardno'),
							'reg_bill_cardcvv' 	=> $this->_request->getPost('cvv'),
							'reg_bill_card_expiry' 	=> $this->_request->getPost('expire_m').'-'.$this->_request->getPost('expire_y'));
				$this->getSessionNamespace()->stepthree = $stepthreedata;
				$data = array();
				
				echo "<pre>";
				foreach($this->getSessionNamespace() as $info)
				{
					print_r($info);
					foreach ($info as $key=>$val)
					{
						$data[$key] = $val;
					}
				}
				
				print_r($this->getSessionNamespace()->stepone);
				print_r($data);
				echo "</pre>";die();
				$users = new AML_Model_Clients();
				$users->insertData($data);	
				$this->_redirect('auth/stepfour');	
			} 
			else 
			{
				$this->view->errors = $form->getMessages();
			}
		}
		
         $this->view->form=$form;
		 $this->_helper->layout->setLayout('index_layout');
		 $this->view->errorMessage = "<spam class='error'>Invalid username or password. Please try again.</spam>";
    } 
	public function stepfourAction()
    {
		 $this->view->regfields = $this->getSessionNamespace();
		 $this->_helper->layout->setLayout('index_layout');
    } 
	public function stepfiveAction()
    {
  		 $this->_helper->layout->setLayout('index_layout');
    }
    public function logoutAction()
    {
        $storage = new Zend_Auth_Storage_Session();
        $storage->clear();
        $this->_redirect('auth/login');
    }
	
	public function indexAction()
    {
		$this->_helper->layout->setLayout('index_layout');
        $storage = new Zend_Auth_Storage_Session();
        $data = $storage->read();
        if(!$data){
            $this->_redirect('auth/login');
        }
       	$this->view->username = $data->username;                
    }

public function termsandcondAction()
    {
		$this->_helper->layout->setLayout('index_layout');
       	$this->view->username = $data->username;                
    }

}

